<div class="row-fluid">
    <?php echo $this->render('/listados/menu.phtml'); ?>

    <div class="well well-small">
        <form name="form_busqueda" class="form-inline noPrint" method="GET" action="<?php echo $this->baseUrl(); ?>/admin/listados/promesas">
            <legend>Buscar Promesas</legend>
            <label class="control-label">Desde</label>
            <input id="desde" name="desde"   value="<?php echo empty($this->desde) ? 1 : $this->desde  ?>" class="input-small" type="text" />
            <label class="control-label">Hasta</label>
            <input id="hasta" name="hasta"   value="<?php echo empty($this->hasta) ? '1' : $this->hasta ?>" class="input-small" type="text" />
                <label class="control-label">Sucursales</label>
                <select id="sucursal_id" name="sucursal_id" class="input-medium">

                    <?php foreach ($this->sucursales as $sucursal) { ?>
                        <?php if (empty($this->sucursal_id)) { ?>
                            <option <?php echo $this->usuario->getSucursal()->getId() == $sucursal->getId() ? "selected" : "" ?> value="<?php echo $sucursal->getId(); ?>">
                            <?php } else { ?>
                            <option <?php echo $this->sucursal_id == $sucursal->getId() ? "selected" : "" ?> value="<?php echo $sucursal->getId(); ?>">
                            <?php } ?>    
                            <?php echo $sucursal->getDescripcion(); ?></option>
                    <?php } ?>
                </select>
            <input class="btn" value="Buscar" type="submit" />
        </form>
        
        <div class="row-fluid">
            Se encontraron <?php echo count($this->promesas); ?> promesas.
           <div class="toolbar pull-right" style="margin: 5px;">
                <button id="generar_listado_promesas" class="btn btn-primary noPrint" onclick="promesasexcel()"><i class=" icon-list icon-white"></i>  Generar Listado Excel</button>
                <!-- window.print() -->
                <button type="button" onclick="PrintElement('#tabla_promesas')" class="btn btn-inverse noPrint"><i class=" icon-print icon-white"></i> Imprimir </button>
            
           </div>
        </div>
        
        <div class="row-fluid ">
           <div id="tabla_promesas_imprimir">
           <table id="tabla_promesas"  class="tabla_reducida">
                <thead>
                    <tr><th>DNI</th><th>Nombre</th><th>Apellido</th><th>Fecha de Promesa</th><th>Verif. Laboral</th><th>Comentario</th><th>Sucursal</th></tr>
                </thead>
                <tbody>
                    <?php if (count($this->promesas)) : ?>
                        <?php
                        foreach ($this->promesas as $promesa) {
                            ?>
                            <tr <?php echo "id='promesa{$promesa['id']}'" ?> >
                                <td style="width:90px;text-align:center;">
                                    <a href="<?php echo $this->baseUrl("usuarios/busqueda/obternerdatospersona") ?>/persona_id/<?php echo $promesa['id']; ?>" onclick="agregaMarcaUrl('<?php echo "#promesa".$promesa['id'] ?>')">
                                    <?php echo $promesa['nroDocumento'] ; ?>
                                    </a>
                                </td>
                                <td><?php echo $promesa['nombres']; ?></td>
                                <td><?php echo $promesa['apellido']; ?></td>
                                <td><?php echo Doctrine_Model_Utilidad::formatoFechaMostrar($promesa['fechaPromesa'] ); ?></td>
                                <td><?php echo Doctrine_Model_Utilidad::formatoFechaMostrar($promesa['fechaVerificacionLaboral'] ); ?></td>
                                <td><?php echo $promesa['comentario']; ?></td>
                                <?php $usuario = $promesa['usuario'] ?>
                                <td><?php echo $usuario->getSucursal()->getDescripcion() //echo $promesa->getUsuario()->getSucursal()->getDescripcion() ?></td>

                            </tr>
                        <?php } ?>
                    <?php else : ?>
                        <tr>
                            <td colspan="7">Sin Resultados</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
               
           </div>
               
        </div>
    </div>
</div>

<script type="text/javascript">
    
    $(function() {
        $("#desde").datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
        $( "#hasta" ).datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
        
        $("#tabla_promesas").chromatable({
            width: "100%",  
            height: "350px",
            scrolling: "yes"
        });
        
    });
    
    function promesasexcel() {
        $.ajax({
            type: "POST",
            url: '<?php echo $this->baseUrl("/admin/listados/promesasexcel") ?>',
            dataType: "html",
            contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
            data: { "f_desde": '<?php echo $this->desde ?>',"f_hasta": '<?php echo $this->hasta ?>', "sucursal_id": '<?php echo $this->sucursal_id ?>' },
            beforeSend: function(){ mensajeEspera(true) },            
            success: function (data) {
                $("#generar_listado_promesas").after(data);
                $("#generar_listado_promesas").remove();
            },
            error: function () {mensajeEspera()}, complete: function () {mensajeEspera()}
       });
    }
    
    function mensajeEspera(value){
    if (value)
        $.blockUI({message: "<h3><img width='50' src='<?php $this->baseUrl("/js/jquery.blockUI/gifts/espera.gif") ?>' />Cargando</h3>"});
    else
        $.unblockUI();
    }

    //Simple wrapper to pass a jQuery object to your new window
    function PrintElement(elem){
        Popup($(elem).html());
    }

    //Creates a new window and populates it with your content
    function Popup(data) {
        //Create your new window
        var w = window.open('', 'Listado Promesas: <?php echo $this->desde ?> - <?php echo $this->hasta ?>', 'height=600,width=800');
        w.document.write('<html><head><title>Listado Promesas <?php echo $this->desde ?> - <?php echo $this->hasta ?></title>');
        //Include your stylesheet (optional)
        w.document.write('<link rel="stylesheet" href="<?php echo $this->baseUrl("bootstrap/css/bootstrap.css") ?>" type="text/css" />');
        w.document.write('</head><body>');
        w.document.write('<h2>Listado Promesas <?php echo $this->desde ?> - <?php echo $this->hasta ?></h2>');
        //Write your content
        w.document.write("<table class='table'>"+data+"</table>");
        w.document.write('</body></html>');
        w.print();
        w.close();

        return true;
    }
</script>